// 使用unocss的规则生成style

import uno from '../../uno.js';

// _u('_w_100', '_h_100', '_s_100', 'width:100px;height:100px', {width:'100px'}, '...')
//  [/^_w_(\d+)$/, ([, d]) => ({ 'width': `${d}px` })],
// [/^_s_(\d+)_?(\d+)?$/, ([, d1, d2]) => `_w_${d1} _h_${d2 || d1}`],
const _uno = uno({
  sr: {
    cmain: '#FFFFFF', // 主题色
    csub: '#FFFFFF', // 主题次色，主要用来做渐变
    ctext: '#000000', // 主题文字色
    chigh: '#000000', // 高亮颜色
    primary: '#409EFF',
    success: '#67C23A',
    info: '#909399',
    warning: '#E6A23C',
    error: '#F56C6C',
  }
});
export const _u = (...list) => {
  let style = {};
  list = list.filter(o => o && o !== true);
  for (const item of list) {
    if (_.isObject(item)) {  // 处理对象格式如：{width:'100px'}
      for (const key in item) {
        style[key] = item[key];
      }
    } else {
      if (item.includes(':') && !item.includes('http:') && !item.includes('https:')) { // 处理'width:100px;height:100px'格式
        const lines = item.split(';').filter(Boolean);
        for (let line of lines) {
          const ss = line.split(':');
          style[ss[0]] = ss[1];
        }
      } else { // 处理uno格式
        const items = item.split(' ').filter(Boolean).map(o => o.replaceAll('#', '')); // 去除颜色的#号，因为在uno.js中会添加上
        for (const item of items) {
          const rule = _.find(_uno.rules, o => _.isRegExp(o[0]) ? o[0].test(item) : item === o[0]);
          if (rule) {
            if (_.isRegExp(rule[0])) {
              const match = item.match(rule[0]);
              style = { ...style, ...(rule[1](match)) };
            } else {
              style = { ...style, ...rule[1] };
            }
          }
        }
      }
    }
  }
  return style;
}

export const _us = (...list) => { // 转化位字符串
  const style = _u(...list);
  return _.map(style, (o, k) => `${k}:${o}`).join(';');
}
